require "mol" if RUBY_PLATFORM == "java"
  
class MoleculesController < ApplicationController

  skip_before_filter :require_login

  # Cache the generated molecule images in public/molecules folder.
  caches_page :show, :if => lambda { |c| c.request.format == "png" || c.request.format == "svg" }

  def index
    @molecules = Molecule.search(params[:term])
    respond_to do |format|
      format.js do
        render :json => @molecules.map { |molecule|
          { :label => "#{molecule.names} (#{molecule.mass} m/z)", :id => molecule.id }
        }.to_json
      end
    end
  end

  def show
    @molecule = Molecule.find(params[:id])
    respond_to do |format|
      format.js
      format.svg { render :text => MoleculeImage.new(@molecule.mol_file).to_svg }
      format.png { render :text => Mol::Renderer.new(@molecule.mol_file).to_png(396, 175) }
    end
  end
end
